package com.usooft.metaTalent.admin.system;

import com.usooft.framework.core.query.Page;
import com.usooft.framework.core.utils.MapBuilder;
import com.usooft.framework.web.auth.RequiresPermission;
import com.usooft.framework.web.auth.RequiresPermissionDependOn;
import com.usooft.metaTalent.admin.system.form.RoleCreateForm;
import com.usooft.metaTalent.admin.system.form.RoleEditForm;
import com.usooft.metaTalent.admin.system.form.RoleListForm;
import com.usooft.metaTalent.core.system.entity.Role;
import com.usooft.metaTalent.core.system.service.RolePermissionService;
import com.usooft.metaTalent.core.system.service.RoleService;
import com.usooft.metaTalent.core.system.wrapper.RoleWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;
    private final RolePermissionService rolePermissionService;

    @GetMapping("/list")
    @RequiresPermission(value = "/system/role/list", description = "角色/查询")
    public ResponseEntity<?> list(RoleListForm form) {
        Page<RoleWrapper> page = roleService.page(form.buildQuery());
        return ResponseEntity.ok(page);
    }

    @PostMapping("/create")
    @RequiresPermission(value = "/system/role/create", description = "角色/添加")
    public ResponseEntity<?> create(RoleCreateForm form) {
        roleService.create(form.getName(), form.getDescription());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/create/get")
    @RequiresPermissionDependOn("/system/role/create")
    public ResponseEntity<?> createGet() {
        return ResponseEntity.ok(MapBuilder.builder()
                .build());
    }

    @PostMapping("/edit")
    @RequiresPermission(value = "/system/role/edit", description = "角色/修改")
    public ResponseEntity<?> edit(RoleEditForm form) {
        Role role = roleService.get(form.getId());
        roleService.edit(role, form.getName(), form.getDescription());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/edit/get")
    @RequiresPermissionDependOn("/system/role/edit")
    public ResponseEntity<?> editGet(Long id) {
        Role role = roleService.get(id);
        return ResponseEntity.ok(MapBuilder.builder()
                .put("source", role)
                .build());
    }

    @PostMapping("/grant")
    @RequiresPermission(value = "/system/role/grant", description = "角色/分配权限")
    public ResponseEntity<?> grant(Long id, String[] permissions) {
        Role role = roleService.get(id);
        rolePermissionService.grant(role, permissions);
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/grant/get")
    @RequiresPermissionDependOn("/system/role/grant")
    public ResponseEntity<?> grantGet(Long id) {
        Role role = roleService.get(id);
        Set<String> permissions = rolePermissionService.listPermissionsByRoleId(role.getId());
        return ResponseEntity.ok(MapBuilder.builder()
                .put("source", MapBuilder.builder()
                        .put("permissions", permissions)
                        .build())
                .build());
    }

}
